package lrscp.lib.swt;

import java.util.ArrayList;

import lrscp.lib.Log;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class InputDialog extends Dialog {

    protected boolean result;
    protected Shell shell;

    String[] keys = new String[] {};
    ArrayList<Text> texts = new ArrayList<Text>();
    String[] inputs = new String[] {};

    private Text text;
    private GridData gdata;
    private String[] defaultValues = new String[0];

    /**
     * Create the dialog.
     * 
     * @param parent
     * @param style
     */
    public InputDialog(Shell parent, int style, String title) {
        super(parent, style);
        if (title == null)
            title = "";
        setText(title);
    }

    public static void main(String[] args) {//
        InputDialog iDlg = new InputDialog(new Shell(), 0, null);
        iDlg.setText("asdf");
        iDlg.setInputKeys(new String[] { "a", "asdffffddddd", "asssddddddddddddddddd", "d" });
        iDlg.open();
        Log.i(iDlg.result);
        Log.i(iDlg.getInputs());
        iDlg.getInputs();
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {
        shell = new Shell(getParent(), getStyle() | SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

        GridLayout gl_shell = new GridLayout(1, false);
        shell.setLayout(gl_shell);

        Composite compositeInput = new Composite(shell, SWT.NONE);
        compositeInput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        compositeInput.setLayout(new GridLayout(2, false));

        createInputTexts(compositeInput);

        Composite compositeButn = new Composite(shell, SWT.NONE);
        compositeButn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
        compositeButn.setLayout(new GridLayout(2, false));

        Button btnOk = new Button(compositeButn, SWT.NONE);
        btnOk.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                result = true;
                shell.dispose();
            }
        });
        btnOk.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
        btnOk.setBounds(0, 0, 72, 22);
        btnOk.setText("确定");

        Button btnCancel = new Button(compositeButn, SWT.NONE);
        btnCancel.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                result = false;
                shell.dispose();
            }
        });
        btnCancel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
        btnCancel.setBounds(0, 0, 72, 22);
        btnCancel.setText("取消");

        shell.addDisposeListener(new DisposeListener() {
            @Override
            public void widgetDisposed(DisposeEvent e) {
                inputs = new String[keys.length];
                for (int i = 0; i < keys.length; i++) {
                    inputs[i] = texts.get(i).getText();
                }
            }
        });

        shell.pack();
        SwtUtils.center(shell, 0);
        shell.setText(getText());
    }

    private void createInputTexts(Composite composit) {
        for (int i = 0; i < keys.length; i++) {
            String key = keys[i];
            Label lblKey = new Label(composit, SWT.NONE);
            lblKey.setText(key + ":");

            text = new Text(composit, SWT.BORDER | SWT.ALPHA);
            if (i < defaultValues.length)
                text.setText(defaultValues[i]);
            gdata = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
            //gdata.widthHint = 100;
            text.setLayoutData(gdata);
            texts.add(text);
        }
    }

    public String[] getInputs() {
        return inputs;
    }

    /**
     * Open the dialog.
     * 
     * @return the result
     */
    public boolean open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    public void setInputKeys(String[] keys) {
        this.keys = keys;
    }

    public void setDefaltValues(String[] values) {
        this.defaultValues = values;
    }
}
